package com.young.shop.controller;

import com.young.shop.dao.UserDao;
import com.young.shop.pojo.entity.User;
import com.young.shop.service.UserService;
import com.young.shop.service.impl.UserServiceImple;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/information")
public class InformationController extends BaseController {
    private UserService userService=new UserServiceImple();
    @Override
    protected void doGet(HttpServletRequest req , HttpServletResponse resp) throws ServletException, IOException {
        User user = paramUser(req);
        System.out.println("information");
        System.out.println(user.getEmail());
        User oldUser = (User)req.getSession().getAttribute("client");
        long id = oldUser.getId();
        try{
            User DBUser = userService.NewOldUser(user , id);
            writerSucccessBaseRespVoToResponse(resp,DBUser);
        }catch (RuntimeException e){
            writerFailedBaseRespVoToResponse(resp,e);
        }












        /*//拿到session ID 知道是那个用户
        HttpSession session = req.getSession();
        User ordUser = (User)session.getAttribute("client");
        long id = ordUser.getId();
        System.out.println("id："+id);
        //得到要修改的参数
        User user = paramUser(req,ordUser);
        System.out.println("修改得到的对象"+user);

        try{
            //根据id去修改对应的用户的数据
            User dbUser = userService.updateUser(id , user);
            //返回数据给前端成功或者失败
            writerSucccessBaseRespVoToResponse(resp,dbUser);
        }catch (RuntimeException e){
            writerFailedBaseRespVoToResponse(resp,e);
        }
    }

    private User paramUser(HttpServletRequest req,User ordUser) {
        User user = new User();

        //如果修改的内容为空，就填入id账号的原属性的值
        String nickname = req.getParameter("nickname");
        if (nickname==null||nickname.trim().equals("")){
            user.setNickName(ordUser.getNickName());
        }
        user.setNickName(nickname);

        String name = req.getParameter("username");
        if (name==null||name.trim().equals("")){
            user.setUserName(ordUser.getUserName());
        }
        user.setRealName(name);
        String sex = req.getParameter("sex");
        if (sex==null||sex.trim().equals("")){
            user.setSex(ordUser.getSex());
        }
        user.setSex(sex);

        String birth = req.getParameter("birth");
        if (birth==null||birth.trim().equals("")){
            user.setBirthday(ordUser.getBirthday());
        }
        user.setBirthday(birth);

        String phone = req.getParameter("userphone");
        if (phone==null||phone.trim().equals("")){
            user.setPhone(ordUser.getPhone());
        }
        user.setPhone(phone);

        String email = req.getParameter("useremail");
        if (email==null||email.trim().equals("")){
            user.setEmail(ordUser.getEmail());
        }
        user.setEmail(email);
        return user;*/
    }

    private User paramUser(HttpServletRequest req) {
        String nickname = req.getParameter("nickname");
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String birth = req.getParameter("birth");
        String userPhone = req.getParameter("userphone");
        String userEmail = req.getParameter("useremail");
        User user = new User();
        user.setNickName(nickname);
        user.setRealName(name);
        user.setSex(sex);
        user.setBirthday(birth);
        user.setPhone(userPhone);
        user.setEmail(userEmail);
        return user;
    }
}
